var canvasBg = document.getElementById("canvasBg"),
    ctxBg = canvasBg.getContext("2d"),
    canvasEntities = document.getElementById("canvasEntities"),
    ctxEntities = canvasEntities.getContext("2d"),
    canvasWidth = canvasBg.width,
    canvasHeight = canvasBg.height,
    player1 = new Player(),
    //enemies = [],
    //numEnemies = 5,
    //obstacles = [],
    isPlaying = false,
    requestAnimFrame =  window.requestAnimationFrame ||
                        window.webkitRequestAnimationFrame ||
                        window.mozRequestAnimationFrame ||
                        window.oRequestAnimationFrame ||
                        window.msRequestAnimationFrame ||
                        function(callback) {
                            window.setTimeout(callback, 1000 / 60);
                        },
    imgSprite = new Image();

imgSprite.src = "images/sprite.png";
imgSprite.addEventListener("load", init, false);


function init() {
    document.addEventListener("keydown", function (e) { checkKey(e, true); }, false);
    document.addEventListener("keyup", function (e) { checkKey(e, false); }, false);
    //defineObstacles();
    //initEnemies();
    begin();
}

function begin() {
    ctxBg.drawImage(imgSprite, 0, 0, canvasWidth, canvasHeight, 0, 0, canvasWidth, canvasHeight);
    isPlaying = true;
   
    requestAnimFrame(loop);
}

function loop() {
    if (isPlaying) {
        update();

        draw();
        requestAnimFrame(loop);
    }
}

function clearCtx(ctx) {
    ctx.clearRect(0,0,canvasWidth, canvasHeight);
}

function update() {
   clearCtx(ctxEntities);
    //updateAllEnemies();
    player1.update();
}

function draw() {
    //drawAllEnemies()
    player1.draw();

    if (player1.isShooting == true) {
        for (var i = player1.curIndex; i < player1.bullets.length; i++) {
            var temp = 0;
            if (player1.bullets[i].Shot == true) {
                player1.bullets[i].draw();
                temp += 1;

            }
        }
        if (temp == 0)
            player1.isShooting = false;

    }
}
function randomRange(min, max) {
    return math.floor(Math.random() * (max + 1 - min)) + min;
}

function Player() {
    this.curIndex = 9;
    this.srcX = 0;
    this.srcY = 600;
    this.width = 35;
    this.height = 54;
    this.drawX = 400;
    this.drawY = 300;
    this.centerX = this.drawX + (this.width / 2);
    this.centerY = this.drawY + (this.height / 2);
    this.speed = 2;
    this.isUpKey = false;
    this.isRightKey = false;
    this.isDownKey = false;
    this.isLeftKey = false;
    this.isSpacebar = false;
    this.isShooting = false;
    this.numBullets = 10;
    this.bullets = [];
    //this.currentBullet = 0;
    for (var i = 0; i < this.numBullets; i++) {
        this.bullets[i] = new Bullet();
    }
    }

    Player.prototype.update = function () {
        this.centerX = this.drawX + (this.width / 2);
        this.centerY = this.drawY + (this.height / 2);
        this.checkDirection();
        //this.checkShooting();
        //this.updateAllBullets();
    };
    Player.prototype.draw = function () {
        //this.drawAllBullets();
        ctxEntities.drawImage(imgSprite, this.srcX, this.srcY, this.width,
            this.height, this.drawX, this.drawY, this.width, this.height);
    };

    Player.prototype.checkDirection = function () {
        var newDrawX = this.drawX,
            newDrawY = this.drawY,
            obstableCollision = false;
        if (this.isUpKey) {
            newDrawY -= this.speed;
            this.srcX = 35; // Facing north
        }
        if (this.isDownKey) {
            newDrawY += this.speed;
            this.srcX = 0; // Facing south
        }
        if (this.isRightKey) {
            newDrawX += this.speed;
            this.srcX = 105; // Facing east
        }
        if (this.isLeftKey) {
            newDrawX -= this.speed;
            this.srcX = 70; // Facing west
        }


        //obstacleCollision = this.checkObstacleCollide(newDrawX, newDrawY);

        if (!obstableCollision && !outOfBounds(this, newDrawX, newDrawY)) {
            this.drawX = newDrawX;
            this.drawY = newDrawY;
        }
    };

    function checkKey(e, value) {
        var keyID = e.keyCode || e.which;
        if (keyID == 38) { // Up arrow
            player1.isUpKey = value;
            e.preventDefault();
        }
        if (keyID == 39) { // Right arrow
            player1.isRightKey = value;
            e.preventDefault();
        }
        if (keyID == 40) { // Down arrow
            player1.isDownKey = value;
            e.preventDefault();
        }
        if (keyID == 37) { // Left arrow
            player1.isLeftKey = value;
            e.preventDefault();
        }
        if (keyID == 32) { // Space arrow
            if (player1.isSpacebar == false) {
                player1.isShooting = value;
                
                if (player1.numBullets > 0){
                    player1.numBullets -= 1;
                }
                if (player1.curIndex > 0) {
                    
                    player1.curIndex -= 1
                    console.log(player1.numBullets + " Bullets left!");
                    player1.bullets[player1.numBullets].Shot = true;
                }
            }
                player1.isSpacebar = value;

            e.preventDefault();
        }
    }

    function outOfBounds(a, x, y) {
        var newBottomY = y + a.height,
            newTopY = y,
            newRightX = x + a.width,
            newLeftX = x,
            treeLineTop = 5,
            treeLineBottom = 570,
            treeLineRight = 750,
            treeLineLeft = 65;

        return newBottomY > treeLineBottom ||
            newTopY < treeLineTop ||
            newRightX > treeLineRight ||
            newLeftX < treeLineLeft;

    }

    function Bullet() {
        this.srcX = 283;
        this.srcY = 618;
        this.width = 38;
        this.height = 29;
        this.drawX = null;
        this.drawY = null;
        this.speed = 2;
        this.isSpacebar = false;
        this.Shot = false;
    }



    Bullet.prototype.draw = function () {
        if (this.drawX == null) {
            this.drawX = player1.drawX;
            this.drawY = player1.drawY;
        }
        ctxEntities.drawImage(imgSprite, this.srcX, this.srcY, this.width,
         this.height, this.drawX, this.drawY, this.width, this.height);

        this.drawX += 4;

        if (player1.bullets[player1.bullets.length - 1].drawX > 800) {
            this.update();
        }
    }

    Bullet.prototype.update = function () {


        if (player1.numBullets > 0) {
            player1.bullets.pop();
        }

    }